package com.tianqu.tms.www.bid.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.CommercialVehicle;
import com.tianqu.tms.entity.VehicleBrand;
import com.tianqu.tms.www.bid.actionform.BidCarSerchInputForm;
import com.tianqu.tms.www.bid.viewhelper.BidCarSerchInputViewHelper;
/**
 * 车辆信息录入-中间跳转页面
 * @author mufeiyu
 *
 */
public class BidCarSerchMiddleAction extends TmsAction {
	// 图片添加
	public static String IMAGESADD = "imagesadd";
	// 车辆添加
	public static String CARADD = "caradd";
	public static String IMAGESMODIFY = "imagesModify";
	// return
	public static String RETURNSERCH = "returnserch";
	public static String IMG1 = "1";
	public static String IMG2 = "2";
	public static String IMG3 = "3";
	public static String IMG4 = "4";
	public static String IMG5 = "5";
	public static String IMG6 = "6";
	public static String IMG7 = "7";
	public static String IMG8 = "8";
	public static String IMGCARD = "9";
	public static String IMGREPORT = "10";
	public static String IMG11 = "11";
	public static String IMG12 = "12";
	public static String IMG13 = "13";
	public static String IMG14 = "14";
	public static String IMG15 = "15";
	public static String IMG16 = "16";
	public static String IMG17 = "17";
	public static String IMG18 = "18";
	public static String IMG19 = "19";
	public static String IMG20 = "20";
	public static String IMG21 = "21";
	public static String IMG22 = "22";// 图片从8张增加到20张  modify by mufeiyu
	
	BidCarSerchInputViewHelper viewHelper = new BidCarSerchInputViewHelper();
	private Logger logger = Logger.getLogger(BidCarSerchMiddleAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		BidCarSerchInputForm carForm = (BidCarSerchInputForm) form;
		BidCarSerchInputViewHelper view = (BidCarSerchInputViewHelper) uc
				.getAttribute(TmsConstants.VIEWHELP);

		viewhel(carForm, view);
		// 判断按钮点击
		if (carForm.getBtns1() != null) {
			// 图1
			viewHelper.setButtionType(IMG1);
		} else if (carForm.getBtns2() != null) {
			// 图2
			viewHelper.setButtionType(IMG2);
		} else if (carForm.getBtns3() != null) {
			// 图3
			viewHelper.setButtionType(IMG3);
		} else if (carForm.getBtns4() != null) {
			// 图4
			viewHelper.setButtionType(IMG4);
		} else if (carForm.getBtns5() != null) {
			// 图5
			viewHelper.setButtionType(IMG5);
		} else if (carForm.getBtns6() != null) {
			// 图6
			viewHelper.setButtionType(IMG6);
		} else if (carForm.getBtns7() != null) {
			// 图7
			viewHelper.setButtionType(IMG7);
		} else if (carForm.getBtns8() != null) {
			// 图8
			viewHelper.setButtionType(IMG8);
		} else if (carForm.getBtnsCard() != null) {
			// 行驶证
			viewHelper.setButtionType(IMGCARD);
		} else if (carForm.getBtnsReport() != null) {
			// 检测报告
			viewHelper.setButtionType(IMGREPORT);
		} else if (carForm.getBtns11() != null) {
			// 图9
			viewHelper.setButtionType(IMG11);
		} else if (carForm.getBtns12() != null) {
			// 图10
			viewHelper.setButtionType(IMG12);
		} else if (carForm.getBtns13() != null) {
			// 图11
			viewHelper.setButtionType(IMG13);
		} else if (carForm.getBtns14() != null) {
			// 图12
			viewHelper.setButtionType(IMG14);
		} else if (carForm.getBtns15() != null) {
			// 图13
			viewHelper.setButtionType(IMG15);
		} else if (carForm.getBtns16() != null) {
			// 图14
			viewHelper.setButtionType(IMG16);
		} else if (carForm.getBtns17() != null) {
			// 图15
			viewHelper.setButtionType(IMG17);
		} else if (carForm.getBtns18() != null) {
			// 图16
			viewHelper.setButtionType(IMG18);
		} else if (carForm.getBtns19() != null) {
			// 图17
			viewHelper.setButtionType(IMG19);
		} else if (carForm.getBtns20() != null) {
			// 图18
			viewHelper.setButtionType(IMG20);
		} else if (carForm.getBtns21() != null) {
			// 图19
			viewHelper.setButtionType(IMG21);
		} else if (carForm.getBtns22() != null) {
			// 图20
			viewHelper.setButtionType(IMG22);// 图片从8张增加到20张  modify by mufeiyu
		} else {
			// 保存
			viewHelper.setSerial(carForm.getSerial());
			request.setAttribute(TmsConstants.VIEWHELP, viewHelper);
			uc.setAttribute(TmsConstants.VIEWHELP, viewHelper);
			return mapping.findForward(CARADD);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
		request.setAttribute(TmsConstants.VIEWHELP, viewHelper);
		uc.setAttribute(TmsConstants.VIEWHELP, viewHelper);

		return mapping.findForward(IMAGESADD);
	}

	/**
	 * view赋值
	 * 
	 * @param form
	 * @return
	 */
	private void viewhel(BidCarSerchInputForm form, BidCarSerchInputViewHelper viewh) throws TmsDaoException{
		viewHelper.setFromType(form.getFromType());
		// 品牌
		viewHelper.setBrandId(form.getBrandId());
		// 车系
		viewHelper.setVendorId(form.getVendorId());
		// 款式
		viewHelper.setCarStyleId(form.getCarStyleId());
		
		CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
		//车辆名称取值
		VehicleBrand brand = VehicleBrand.getInstance();
		String brandIdStr = "";
		if(StringUtil.isNullOrBlank(form.getCarBrand())){
			brandIdStr = form.getBrandId();
		}else{
			brandIdStr = form.getCarBrand();
		}
		
		if (StringUtil.isNullOrBlank(form.getCarBrand()) && StringUtil.isNullOrBlank(form.getBrandId())) {
			viewHelper.setCarName(" ");
			viewHelper.setCountryStyle("");
		} else {
			List list = new ArrayList();
			if(form.getFromType() == 1){
				list = brand.getAllVehicleBrandInfo(form.getVendorId());
			}else{
				list = commercialVehicle.getAllVehicleBrandInfo(form.getVendorId());
			}
			
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					Map map = (Map) list.get(i);
					if (map.get("brandId").equals(brandIdStr)) {
						String carName = (String) map.get("brandName");
						viewHelper.setCarName(carName);
					}
				}
			}else{
				viewHelper.setCarName(" ");
			}
		}
		
		// 车辆基本信息
		viewHelper.setCarOwner(form.getCarOwner());
		viewHelper.setVin(StringUtil.toUpperCase(form.getVin()));
		viewHelper.setLicensePlate(StringUtil.toUpperCase(form.getLicensePlate()));
		viewHelper.setColor(form.getColor());
		viewHelper.setRegDate(form.getRegDate());
		viewHelper.setYearCarefulPeriod(form.getYearCarefulPeriod());
		viewHelper.setJqx(form.getJqx());
		viewHelper.setContactPerson(form.getContactPerson());
		viewHelper.setContactPhone(form.getContactPhone());
		viewHelper.setCarOwnerIdCardNo(form.getCarOwnerIdCardNo());
		viewHelper.setOwnerType(form.getOwnerType());
		viewHelper.setOwnerPhone(form.getOwnerPhone());
		viewHelper.setOwnerAddress(form.getOwnerAddress());
		
		// 图片
		viewHelper.setImages1(form.getImages1());
		viewHelper.setImages2(form.getImages2());
		viewHelper.setImages3(form.getImages3());
		viewHelper.setImages4(form.getImages4());
		viewHelper.setImages5(form.getImages5());
		viewHelper.setImages6(form.getImages6());
		viewHelper.setImages7(form.getImages7());
		viewHelper.setImages8(form.getImages8());
		viewHelper.setImages9(form.getImages9());
		viewHelper.setImages10(form.getImages10());
		viewHelper.setImages11(form.getImages11());
		viewHelper.setImages12(form.getImages12());
		viewHelper.setImages13(form.getImages13());
		viewHelper.setImages14(form.getImages14());
		viewHelper.setImages15(form.getImages15());
		viewHelper.setImages16(form.getImages16());
		viewHelper.setImages17(form.getImages17());
		viewHelper.setImages18(form.getImages18());
		viewHelper.setImages19(form.getImages19());
		viewHelper.setImages20(form.getImages20());
		viewHelper.setImages21(form.getImages21());
		viewHelper.setImages22(form.getImages22());// 图片从8张增加到20张  modify by mufeiyu

		// 竞价类型
		viewHelper.setBidType(form.getBidType());
		// 起拍时间
		viewHelper.setStartTime(String.valueOf(form.getStartTime()));
		// 品牌List
		viewHelper.setVendorList(viewh.getVendorList());

		// 车辆类型list
		viewHelper.setCarTypeList(viewh.getCarTypeList());

		// 颜色list
		viewHelper.setColorList(viewh.getColorList());

		// 车主类型list
		viewHelper.setOwnerTypeList(viewh.getOwnerTypeList());

		viewHelper.setOwnerSex(form.getOwnerSex());
		viewHelper.setOwnerBirthday(form.getOwnerBirthday());
		
		if("0".equals(form.getPrice())){
			viewHelper.setPrice("");
		}else{
			viewHelper.setPrice(form.getPrice().replaceAll(",",""));
		}
		
		if("0".equals(form.getMileage())){
			viewHelper.setMileage("");
		}else{
			viewHelper.setMileage(form.getMileage().replaceAll(",",""));
		}
		
		if("0".equals(String.valueOf(form.getAuctionDeadline()))){
			viewHelper.setAuctionDeadline("");
		}else{
			viewHelper.setAuctionDeadline(String.valueOf(form.getAuctionDeadline()));
		}

		viewHelper.setSerial(form.getSerial());
	}
}
